package views

import (
	"blog/common"
	"blog/context"
	"blog/service"
	"errors"
	"log"
	"strconv"
	"strings"
)

func (*HTMLApi) Index(context *context.MsContext) {
	index := common.Template.Index
	pageStr, err := context.GetForm("page")
	if err != nil {
		log.Println("表单获取失败")
		index.WriteData(context.W, errors.New("系统错误，请联系管理员！！！"))
	}
	page := 1
	if pageStr != "" {
		page, _ = strconv.Atoi(pageStr)
	}
	//每页显示的数量
	pageSize := 5
	path := context.Request.URL.Path
	slug := strings.TrimPrefix(path, "/")
	hr, err := service.GetAllIndexInfo(slug, page, pageSize)
	if err != nil {
		log.Println("首页获取数据出错")
		index.WriteError(context.W, errors.New("系统错误，请联系管理员！！！"))
	}
	index.WriteData(context.W, hr)
}
